﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jumpton.SFA.Server.Entity;
using BusinessRule.Promotion;
using System.Web.Services;
using Jumpton.SFA.Server.BusinessRule;
using System.Runtime.Serialization.Json;

public partial class Promotion_Promotions : PageBase
{
    protected string ddl_Promotion = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddl_Promotion = EnumResources.LoadJsonString(EnumType.PromotionType);
        }
    }

    [WebMethod]
    public static string BindData(decimal d)
    {
        PromotionsManager pm = new PromotionsManager();
        pm.Domain_id = d;
        
        return pm.GetTableJsonString();
    }

    [WebMethod]
    public static string Svae(int i, decimal d, string l, string c, string n, string s, string e, string de)
    {
        PromotionsManager pm = new PromotionsManager();
        pm.Domain_id = d;

        PROMOTIONS pr = null;
        if (i == -1)
        {
            if (pm.IsExists(c, n))
            {
                return "";
            }
            pr = new PROMOTIONS();
            pr.DOMAINID = d;
            pr.CREATETIME = BusinessLogic.GetDateTimeLong(DateTime.Now);
        }
        else
        {
            if (pm.IsExists(c, n, i))
            {
                return "";
            }
            pr = pm.Load(i);
        }

        if (s == "-1" && e == "-1")
            pr.ACTIVE = false;
        else
        {
            pr.ACTIVE = true;
            pr.CODE = c.Replace("’", "'");
            pr.NAME = n.Replace("’", "'");
            if (!string.IsNullOrEmpty(s))
                pr.STARTDATE = BusinessLogic.GetDateTimeDecimal(DateTime.Parse(s));
            if (!string.IsNullOrEmpty(e))
                pr.ENDDATE = BusinessLogic.GetDateTimeDecimal(DateTime.Parse(e));
            pr.DESCRIPTION = de.Replace("’", "'");
        }
        pr.LASTUPDATER = l;
        pr.LASTUPDATETIME = BusinessLogic.GetDateTimeLong(DateTime.Now);


        return pm.Save(pr) ? "1" : "0";
    }
}